//
//  UIButton+Utils.m
//  iOSTE
//
//  Created by LL on 2021/9/14.
//

#import "UIButton+Utils.h"

@implementation UIButton (Utils)

- (CGFloat)w_textWidth {
    return [self w_textWidthForState:UIControlStateNormal];
}

- (CGFloat)w_textHeight {
    return [self w_textHeightForState:UIControlStateNormal];
}

- (CGSize)w_textSize {
    return [self w_textSizeForState:UIControlStateNormal];
}

- (CGFloat)w_textWidthForState:(UIControlState)state {
    NSAttributedString *attributedText = [self attributedTitleForState:state];
    if (attributedText) {
        return [TL_ViewHelper calcTextSize:CGSizeMake(CGFLOAT_MAX, 30.0)
                                 text:attributedText
                        numberOfLines:1
                                 font:nil
                        textAlignment:attributedText.alignment
                        lineBreakMode:attributedText.lineBreakMode
                   minimumScaleFactor:0
                         shadowOffset:CGSizeZero].width;
    }
    
    NSString *text = [self titleForState:state];
    if (text) {
        return [TL_ViewHelper calcTextSize:CGSizeMake(CGFLOAT_MAX, 30.0)
                                 text:text
                        numberOfLines:1
                                 font:self.titleLabel.font
                        textAlignment:self.titleLabel.textAlignment
                        lineBreakMode:self.titleLabel.lineBreakMode
                   minimumScaleFactor:0
                         shadowOffset:CGSizeZero].width;
    }
        
        return 0;
}

- (CGFloat)w_textHeightForState:(UIControlState)state {
    NSAttributedString *attributedText = [self attributedTitleForState:state];
    if (attributedText) {
        return [TL_ViewHelper calcTextSize:CGSizeMake(self.titleLabel.preferredMaxLayoutWidth, 30.0)
                                 text:attributedText
                        numberOfLines:self.titleLabel.numberOfLines
                                 font:nil
                        textAlignment:attributedText.alignment
                        lineBreakMode:attributedText.lineBreakMode
                   minimumScaleFactor:0
                         shadowOffset:CGSizeZero].height;
    }
    
    NSString *text = [self titleForState:state];
    if (text) {
        return [TL_ViewHelper calcTextSize:CGSizeMake(self.titleLabel.preferredMaxLayoutWidth, 30.0)
                                 text:text
                        numberOfLines:self.titleLabel.numberOfLines
                                 font:self.titleLabel.font
                        textAlignment:self.titleLabel.textAlignment
                        lineBreakMode:self.titleLabel.lineBreakMode
                   minimumScaleFactor:0
                         shadowOffset:CGSizeZero].height;
    }
    
    return 0;
}

- (CGSize)w_textSizeForState:(UIControlState)state {
    NSAttributedString *attributedText = [self attributedTitleForState:state];
    CGFloat maxWidth = self.titleLabel.preferredMaxLayoutWidth;
    if (maxWidth == 0) maxWidth = CGFLOAT_MAX;
    
    if (attributedText) {
        return [TL_ViewHelper calcTextSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                                 text:attributedText
                        numberOfLines:self.titleLabel.numberOfLines
                                 font:nil
                        textAlignment:attributedText.alignment
                        lineBreakMode:attributedText.lineBreakMode
                   minimumScaleFactor:0
                         shadowOffset:CGSizeZero];
    }
    
    NSString *text = [self titleForState:state];
    if (text) {
        return [TL_ViewHelper calcTextSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                                 text:text
                        numberOfLines:self.titleLabel.numberOfLines
                                 font:self.titleLabel.font
                        textAlignment:self.titleLabel.textAlignment
                        lineBreakMode:self.titleLabel.lineBreakMode
                   minimumScaleFactor:0
                         shadowOffset:CGSizeZero];
    }
    
    return CGSizeZero;
}

@end
